package com.park.interceptor;

import com.park.common.annotation.Protected;
import com.park.common.constant.IPlatformConstant;
import com.park.common.poo.UserOperator;
import com.park.controller.BaseController;
import com.park.platform.service.IOperatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

import static com.park.interceptor.InterceptorHelp.NOT_LOGIN;
import static com.park.interceptor.InterceptorHelp.isAjax;

public class AuthorizationHandlerInterceptor extends BaseController implements HandlerInterceptor {

    @Autowired
    private IOperatorService operatorService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        
    	//if(1==1) return true; //方便测试，后期删除！
        	
    	try{
            if (!(handler instanceof HandlerMethod)) {
                return true;
            }
            if(super.getUserInfo2() == null){
                try{
                    UserOperator userOperator = super.getUserOperatorCookie();
                    if(userOperator != null){
                        request.getSession().setAttribute(IPlatformConstant.LOGIN_USER, operatorService.login(userOperator.getOperatorMobile(), userOperator.getOperatorPwd()));
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            HandlerMethod handlerMethod = (HandlerMethod) handler;
            // 标记保护权限的Controller和方法，在没有登录的情况下跳转至登录页面
            Protected classAnnotation = handlerMethod.getBeanType().getAnnotation(Protected.class);
            Protected methodAnnotation = handlerMethod.getMethod().getAnnotation(Protected.class);
            if (classAnnotation != null || methodAnnotation != null) {
                if (request.getSession().getAttribute(IPlatformConstant.LOGIN_USER) == null) {
                    if(isAjax(request)){
                        PrintWriter out = response.getWriter();
                        out.write(NOT_LOGIN); //这里必须是数字, 不能是字符串(字符串会解析成js变量)
                        out.close();
                        return false;
                    }else {
                        //request.getRequestDispatcher(loginType(request)+"?returnUrl="+RequestUtil.getRequestURIQuery(request)).forward(request, response);
                        response.sendRedirect("/");
                        return false;// 终止拦截器继续传播
                    }
                }
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //request.setAttribute("static_resource_version", siteVersionHolder.getVersion());
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
